/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arc.level;

import com.jme3.math.Quaternion;
import com.jme3.math.Transform;
import com.jme3.math.Vector3f;
import com.jme3.network.serializing.Serializable;

/**
 *
 * @author Adam
 */
@Serializable
public class NetworkedTransform {
    float[] rotation;
    float[] translation;
    float[] scale;
    
    public NetworkedTransform(){
        translation = new float[3];
        rotation = new float[4];
        scale = new float[3];
        translation[0] = 0;
        translation[1] = 0;
        translation[2] = 0;
        rotation[0] = 0;
        rotation[1] = 0;
        rotation[2] = 0;
        scale[0] = 0;
        scale[1] = 0;
        scale[2] = 0;
        
    }
    
    public NetworkedTransform(Transform t){
        if(t == null){
            throw new NullPointerException("NetworkedTransform: Cannot construct from null transform.");
        }else{
            translation = new float[3];
            rotation = new float[4];
            scale = new float[3];

            translation[0] = t.getTranslation().x; translation[1] = t.getTranslation().y; translation[2] = t.getTranslation().z;  
            rotation[0] = t.getRotation().getX(); rotation[1] = t.getRotation().getY(); 
            rotation[2] = t.getRotation().getZ();  rotation[3] = t.getRotation().getW();
            scale[0] = t.getScale().x; scale[1] = t.getScale().y; scale[2] = t.getScale().z;
        }
    }
    
    public Transform toTransform(){
        Vector3f trans = new Vector3f(translation[0], translation[1], translation[2]);
        Quaternion rot = new Quaternion(rotation[0], rotation[1], rotation[2], rotation[3]);
        Vector3f cScale = new Vector3f(scale[0], scale[1], scale[2]);
        
        return new Transform(trans, rot, cScale);
    }
    
    public void setTranslation(Vector3f t){
        setTranslation(t.getX(), t.getY(), t.getZ());
    }
    
    public void setTranslation(float x, float y, float z){
        translation[0] = x;
        translation[1] = y;
        translation[2] = z;
    }
    
    public Vector3f getTranslation(){
        return new Vector3f(translation[0], translation[1], translation[2]);
    }
    
    public void setRotation(Quaternion q){
        setRotation(q.getX(), q.getY(), q.getZ(), q.getW());
    }
    
    public void setRotation(float x, float y, float z, float w){
        rotation[0] = x;
        rotation[1] = y;
        rotation[2] = z;
        rotation[3] = w;
    }
    
    public Quaternion getRotation(){
        return new Quaternion(rotation[0], rotation[1], rotation[2], rotation[3]);
    }
    
    public void setScale(Vector3f s)
    {
        setScale(s.getX(), s.getY(), s.getZ());
    }
    
    public void setScale(float x, float y, float z){
        scale[0] = x;
        scale[1] = y;
        scale[2] = z;
    }
    
    public Vector3f getScale(){ return new Vector3f(scale[0], scale[1], scale[2]); }
}
